package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;

    /**
     * 商品分页查询
     * @param page
     * @param size
     * @param saleable
     * @param categoryId
     * @param brandId
     * @param id
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page",required = true,defaultValue = "1")Integer page,
            @RequestParam(value = "rows",required = true,defaultValue = "5")Integer size,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id
    ){
        return ResponseEntity.ok(spuService.querySpuByPage(page,size,saleable,categoryId,brandId,id));
    }

    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("spu")
    public ResponseEntity<Void> saveGood(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("spu")
    public ResponseEntity<Void> updateGood(@RequestBody SpuDTO spuDTO){
        spuService.updateGood(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 上下架商品
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id")Long id,
            @RequestParam("saleable")Boolean saleable
    ){
        spuService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询商品信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> getSpuById(@PathVariable("id")Long id){
        return ResponseEntity.ok(spuService.getSpuById(id));
    }

    /**
     * 根据id查询detail
     * @param id
     * @return
     */
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> getSpuDetailById(@RequestParam("id")Long id){
        return ResponseEntity.ok(new SpuDetailDTO(spuDetailService.getById(id)));
    }

    /**
     * 根据spuID查询sku
     * @param spuId
     * @return
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> getSkusBySpuId(@RequestParam("id")Long spuId){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.query().eq("spu_id",spuId).list()));
    }

    /**
     * 根据id集合查询SKU集合
     * @param ids
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> getSkusByIds(@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    /**
     * 查询商品规格参数键值对
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecValues(@RequestParam("id")Long id,
                                                              @RequestParam(value = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(spuDetailService.querySpecValues(id,searching));
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    /**
     * 扣减库存
     * @param cartMap 商品集合
     * @return
     */
    @PutMapping("stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long,Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }

    /**
     * 加库存
     * @param cartMap 商品id及数量的map
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> addStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.addStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
